#include "savepostipsdialog.h"
#include "ui_savepostipsdialog.h"
#include "FramelessHelper/FramelessHelper.h"
#include <QSettings>

SavePosTipsDialog::SavePosTipsDialog(const QString& posKey, QWidget *parent) :
    QDialog(parent, Qt::FramelessWindowHint),
    ui(new Ui::SavePosTipsDialog),
    m_posKey(posKey) // 初始化成员变量
{
    ui->setupUi(this);
    auto helper = new FramelessHelper(this);
    helper->setDraggableMargins(3, 3, 3, 3);
    helper->setMaximizedMargins(3, 3, 3, 3);
    helper->setTitleBarHeight(32);

    helper->addExcludeItem(ui->closeButton);
    connect(ui->closeButton, &QPushButton::clicked,
            helper, &FramelessHelper::triggerCloseButtonAction);

    setAttribute(Qt::WA_StyledBackground, true);
    setStyleSheet("background-color: white;");

    // 从配置文件中读取对应位置的名称并显示
    QSettings settings("settings.ini", QSettings::IniFormat);
    QString name = settings.value(QString("SavedPos/%1/Name").arg(m_posKey)).toString();
    ui->lineEdit->setText(name);

    connect(ui->pushButton_cover, &QPushButton::clicked, this, &SavePosTipsDialog::onCoverClicked);
    connect(ui->pushButton_del, &QPushButton::clicked, this, &SavePosTipsDialog::onDeleteClicked);
    connect(ui->pushButton_rename, &QPushButton::clicked, this, &SavePosTipsDialog::onRenameClicked);
    connect(ui->pushButton_cancel, &QPushButton::clicked, this, &SavePosTipsDialog::close);
}

SavePosTipsDialog::~SavePosTipsDialog()
{
    delete ui;
}

void SavePosTipsDialog::onCoverClicked() {
    emit coverClicked();
    accept(); // 关闭对话框
}

void SavePosTipsDialog::onDeleteClicked() {
    emit deleteClicked();
    accept(); // 关闭对话框
}

void SavePosTipsDialog::onRenameClicked() {
    // 获取参考名称和相对位置信息（假设通过 UI 输入）
    QString referenceName = ui->lineEdit->text(); 

    emit renameClicked(referenceName);
    accept(); // 关闭对话框
}
